﻿using Qy.Common;
using Qy.Models;
using PetaPoco;
using PetaPoco.Core;
using System.Threading.Tasks;

namespace Qy.Repository
{
    [AppService]
    public class ConfigRepository : BaseRepository<Config, PetaPocoContext>
    {

        public ConfigRepository(PetaPocoContext petaPocoContext) : base(petaPocoContext) { }

        public async Task<Config> GetByNameAsync(string name)
        {
            PocoData data = PocoData.ForType(typeof(Config), new ConventionMapper());
            Sql sql = Sql.Builder.Select("*");
            sql.From(new object[] { data.TableInfo.TableName });
            sql.Where("Name = @0", name);
            return await CreateDAO().SingleOrDefaultAsync<Config>(sql);
        }

        public virtual async Task<int> UpdateByNameAsync(string systemInfo, string upLoad, string commonlyUsed)
        {
            Sql sql = Sql.Builder.Append("UPDATE qy_config SET data = (CASE  WHEN name = 'SystemInfo' THEN '" + systemInfo + "' ");
            sql.Append(" WHEN name = 'UpLoad' THEN '" + upLoad + "' ");
            sql.Append(" WHEN name = 'CommonlyUsed' THEN '" + commonlyUsed + "'  END)");
            sql.Append(" WHERE name IN ('SystemInfo','UpLoad','CommonlyUsed')");

            return await CreateDAO().ExecuteAsync(sql);
        }
    }
}
